package com.pointi.emoTools;

import java.io.File;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;

import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;

public class emoDefine
{
	public emoDefine()
	{
		//
	}

	// ____________________________________________________________________________________________________
	// Data Converting

	// ____________________________________________________________________________________________________
	// BYTE and SHORT

	public short [] byte2short (byte [] mIN)
	{
		ByteBuffer buf = ByteBuffer.wrap(mIN).order(ByteOrder.LITTLE_ENDIAN) ;
		short [] mOUT = new short [mIN.length / 2] ;
		for (int i=0 ; i<mOUT.length ; i++) { mOUT[i] = buf.getShort(i * 2) ; }
		return mOUT ;
	}
	public byte [] short2byte (short [] mIN)
	{
		byte [] mOUT = new byte [mIN.length * 2] ;
		ByteBuffer buf = ByteBuffer.wrap(mOUT).order(ByteOrder.LITTLE_ENDIAN) ;
		for(short l : mIN) { buf.putShort(l) ; }
		return mOUT ;
	}

	// ____________________________________________________________________________________________________
	// BYTE and DOUBLE

	public byte [] double2byte (double [] mIN)
	{
		if (mIN == null) return null ;
		byte [] mOUT = new byte [mIN.length * 8] ;
		ByteBuffer buf = ByteBuffer.wrap(mOUT).order(ByteOrder.LITTLE_ENDIAN) ;
		for(double l : mIN) { buf.putDouble(l) ; }
		return mOUT ;
	}
	public double [] byte2double (byte [] mIN)
	{
		ByteBuffer buf = ByteBuffer.wrap(mIN).order(ByteOrder.LITTLE_ENDIAN) ;
		double [] mOUT = new double [mIN.length / 8] ;
		for (int i=0 ; i<mOUT.length ; i++) { mOUT[i] = buf.getDouble(i * 8) ;}
		return mOUT ;
	}

	// ____________________________________________________________________________________________________
	// CODEC | G.711 / G.723 / G.729

	public byte [] encodeData (byte [] mIN)
	{
		byte [] mOUT  = new byte [mIN.length] ;
		return mOUT ;
	}
	public byte [] decodeData (byte [] mIN)
	{
		byte [] mOUT  = new byte [mIN.length] ;
		return mOUT ;
	}

	// ____________________________________________________________________________________________________
	// Read File

	public byte [] readFile(String fileName)
	{
		File soundFile = new File(fileName) ;
		AudioInputStream audioInputStream = null ;
		try { audioInputStream = AudioSystem.getAudioInputStream(soundFile) ; }
		catch (Exception e) { e.printStackTrace() ; System.exit(1) ; }
		int nFileSize = (int)audioInputStream.getFrameLength() * 2  ;
		byte [] pbLawData  = new byte [nFileSize] ;
		try { audioInputStream.read(pbLawData, 0, pbLawData.length) ; }
		catch (IOException e) { e.printStackTrace() ; }
		return pbLawData ;
	}
}
